import { createStore } from 'vuex'

export default createStore({
  state: {
    businessId: localStorage.getItem('businessId') || null,
    businessName: localStorage.getItem('businessName') || null,
    token: localStorage.getItem('businessToken') || null,
    shopInfo: null,
    foods: [],
    orders: []
  },
  
  getters: {
    isLoggedIn: (state) => !!state.token,
    businessId: (state) => state.businessId,
    shopInfo: (state) => state.shopInfo
  },
  
  mutations: {
    setBusinessInfo(state, { businessId, businessName, token }) {
      state.businessId = businessId
      state.businessName = businessName
      state.token = token
      localStorage.setItem('businessId', businessId)
      localStorage.setItem('businessName', businessName)
      localStorage.setItem('businessToken', token)
    },
    
    setShopInfo(state, shopInfo) {
      state.shopInfo = shopInfo
    },
    
    setFoods(state, foods) {
      state.foods = foods
    },
    
    setOrders(state, orders) {
      state.orders = orders
    },
    
    logout(state) {
      state.businessId = null
      state.businessName = null
      state.token = null
      state.shopInfo = null
      localStorage.removeItem('businessId')
      localStorage.removeItem('businessName')
      localStorage.removeItem('businessToken')
    }
  },
  
  actions: {
    login({ commit }, { businessId, businessName, token }) {
      commit('setBusinessInfo', { businessId, businessName, token })
    },
    
    logout({ commit }) {
      commit('logout')
    }
  }
})
